import 'package:get/get.dart';
import 'package:squat_easy/generated/assets.dart';
import 'package:squat_easy/se_main/se_history/se_history_logic.dart';
import 'package:squat_easy/se_main/se_history/se_history_page.dart';
import 'package:squat_easy/se_main/se_home/se_home_logic.dart';
import 'package:squat_easy/se_main/se_home/se_home_page.dart';
import 'package:squat_easy/se_main/se_mine/se_mine_logic.dart';
import 'package:squat_easy/se_main/se_mine/se_mine_page.dart';
import 'package:squat_easy/se_main/se_video/se_video_logic.dart';
import 'package:squat_easy/se_main/se_video/se_video_page.dart';
import 'package:squat_easy/se_ui/se_home_scaffold.dart';
import 'package:ylm_ui_kit/ylm_ui_kit.dart';

class SeMainLogic extends GetxController with SeNavigator, SeRefreshUnread {
  bool hasInitData = false;

  @override
  void onReady() {
    super.onReady();
    initData();
  }

  @override
  void onClose() {
    Get.delete<SeHomeLogic>();
    Get.delete<SeVideoLogic>();
    Get.delete<SeHistoryLogic>();
    Get.delete<SeMineLogic>();
    super.onClose();
  }

  void initData() async {
    hasInitData = true;

    update();
  }

  @override
  void refreshDataAnyway() {}
}

///刷新未读
mixin SeRefreshUnread on SeNavigator {
  @override
  String badgeOfHome() {
    return '';
  }

  @override
  String badgeOfVideo() {
    return '';
  }

  @override
  String badgeOfHistory() {
    return '';
  }

  @override
  String badgeOfMine() {
    return '';
  }
}

///底部tab
mixin SeNavigator implements CommonLife {
  final CommonHomeScaffoldController controller = CommonHomeScaffoldController(0);

  final _homePage = SeHomePage(Get.put(SeHomeLogic()));
  final _videoPage = SeVideoPage(Get.put(SeVideoLogic()));
  final _historyPage = SeHistoryPage(Get.put(SeHistoryLogic()));
  final _minePage = SeMinePage(Get.put(SeMineLogic()));

  SeHomeLogic get homeLogic => _homePage.logic;

  SeVideoLogic get videoLogic => _videoPage.logic;

  SeHistoryLogic get historyLogic => _historyPage.logic;

  SeMineLogic get myLogic => _minePage.logic;

  int _curPage = 0;

  int get curPage => _curPage;

  List<CommonHomeScaffoldItem> get pages => [
        CommonHomeScaffoldItem(
          _homePage,
          '',
          Assets.svgTabHomeNormal,
          Assets.svgTabHomeSelect,
          badgeOfHome,
          homeLogic,
        ),
        CommonHomeScaffoldItem(
          _videoPage,
          '',
          Assets.svgTabVideoNormal,
          Assets.svgTabVideoSelect,
          badgeOfVideo,
          videoLogic,
        ),
        CommonHomeScaffoldItem(
          _historyPage,
          '',
          Assets.svgTabHistoryNormal,
          Assets.svgTabHistorySelect,
          badgeOfHistory,
          historyLogic,
        ),
        CommonHomeScaffoldItem(
          _minePage,
          '',
          Assets.svgTabProfileNormal,
          Assets.svgTabProfileSelect,
          badgeOfMine,
          myLogic,
        ),
      ];

  void changePage(int page) {
    page = page >= pages.length ? pages.length - 1 : page;
    page = page < 0 ? 0 : page;
    pageLeave();
    _curPage = page;
    pageBack();
  }

  ///切换tab
  void changeTabIndex(int index) {
    if (index < 0 || index >= pages.length) return;
    controller.changeIndex(index);
  }

  CommonLife get _curLogic => pages[_curPage].logic;

  ///页面离开
  @override
  void pageLeave() {
    _curLogic.pageLeave();
  }

  ///页面重显
  @override
  void pageBack() {
    _curLogic.pageBack();
    refreshDataAnyway();
  }

  ///页面关闭
  @override
  void pageClose() {
    _curLogic.pageClose();
  }

  void refreshDataAnyway();

  String badgeOfHome();

  String badgeOfVideo();

  String badgeOfHistory();

  String badgeOfMine();
}
